package com.novel.controller.novel;


import com.github.pagehelper.PageInfo;
import com.novel.common.exception.CustomerException;
import com.novel.common.vo.NovelInfo;
import com.novel.controller.other.BaseController;
import com.novel.pojo.novel.Novel;
import com.novel.service.NovelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/novel")
public class NovelController extends BaseController {

    @Autowired
    private NovelService novelService;


    /**
     * 获取小说各种信息
     */
    @PostMapping("param/{id}")
    public NovelInfo getNovelInfo(@PathVariable("id") String novelId) {

        Novel novel = novelService.findNovelById(novelId);

        if (Objects.isNull(novel)) {
            throw new CustomerException("没有找到对应的小说");
        }

        return NovelInfo.parseNovelInfo(novel);
    }


    /**
     * 分页查询小说信息
     * 根据传入的小说内容信息
     *
     * @param pageNum  当前页号
     * @param category 小说类型
     */
    @GetMapping("/page")
    public ResponseEntity<PageInfo<Novel>> findAllNovelByPage(
            @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(required = false, defaultValue = "4") Integer pageSize,
            @RequestParam(required = false) String category) {
        //查询数据
        PageInfo<Novel> pageInfo = novelService.findAllNovelByPage(pageNum, pageSize, category);

        //返回数据给页面
        return ResponseEntity.ok(pageInfo);
    }

    /**
     * 根据书名模糊查询书籍列表
     *
     * @param novelName 模糊书名
     */
    @GetMapping("/name")
    public List<Novel> findNovelByName(String novelName) {
        return novelService.findNovelByName(novelName);
    }

    /**
     * 根据书籍id查询书籍基本信息
     *
     * @param id 书籍id
     */
    @GetMapping("/{id}")
    public Novel findNovelById(@PathVariable("id") String id) {
        Novel novel = novelService.findNovelById(id);
        return novel;
    }

    /**
     * 根据作者名字查询书籍列表信息
     *
     * @param authorName 作者名字
     */
    @GetMapping("/author")
    public List<Novel> findNovelByAuthor(String authorName) {
        return novelService.findNovelByAuthor(authorName);
    }

}
